package com.tanhua.dubbo.api;

import com.tanhua.dubbo.api.mongo.VideoApi;
import com.tanhua.dubbo.utils.IdWorker;
import com.tanhua.model.mongo.Video;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.ArrayList;
import java.util.List;

/**
 * 小视频实现类
 */
@DubboService
public class VideoApiImpl implements VideoApi {

    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private IdWorker idWorker;

    /**
     * 保存视频
     *
     * @param video
     */
    @Override
    public void save(Video video) {
        video.setVid(idWorker.getNextId("video"));
        video.setCreated(System.currentTimeMillis());
        mongoTemplate.insert(video);
    }

    /**
     * 根据userId获取视频列表
     *
     * @param userId
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageResult<Video> getByUserId(Long userId, Long page, Long pageSize) {
        Query query = new Query(Criteria.where("userId").is(userId));
        long count = mongoTemplate.count(query, Video.class);
        long start = (page - 1) * pageSize;
        List<Video> videos = new ArrayList<>();
        if(count > start) {
            query.skip(start).limit(pageSize.intValue())
                    .with(Sort.by(Sort.Direction.DESC, "created"));
            videos = mongoTemplate.find(query, Video.class);
        }
        return new PageResult<>(page, pageSize, count, videos);
    }

    /**
     * 根据vids查询视频列表
     *
     * @param vids
     * @return
     */
    @Override
    public List<Video> getByVids(List<Long> vids) {
        Query query = new Query(Criteria.where("vid").in(vids));
        return mongoTemplate.find(query, Video.class);
    }

    /**
     * 分页查询
     *
     * @param userId
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageResult<Video> findPage(Long userId, Long page, Long pageSize) {
        Query query = new Query(Criteria.where("userId").ne(userId));
        long count = mongoTemplate.count(query, Video.class);
        long start = (page - 1) * pageSize;
        List<Video> videos = new ArrayList<>();
        if(count > start) {
            query.skip(start).limit(pageSize.intValue())
                    .with(Sort.by(Sort.Direction.DESC, "created"));
            videos = mongoTemplate.find(query, Video.class);
        }
        return new PageResult<>(page, pageSize, count, videos);
    }

    /**
     * 修改点赞数量
     *
     * @param id
     * @param count
     */
    @Override
    public Integer updateLikeCount(ObjectId id, int count) {
        Query query = new Query(Criteria.where("_id").is(id));
        Update update = new Update();
        update.inc("likeCount", count);
        FindAndModifyOptions famo = new FindAndModifyOptions();
        famo.returnNew(true);
        Video video = mongoTemplate.findAndModify(query, update, famo, Video.class);
        return video.getLikeCount();
    }
}
